package org.jeecgframework.web.system.pojo.base;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.ExcelUtil;


/**
 * 
 * @author xiul
 * 2016-6-17 上午10:52:19
 * Description 检查完SN之后 保存数据
 */
public class ZyUpFactoryRecordThread extends Thread{
	
	private XSSFSheet sheet;
	private int begin;
	private int end;
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");

	public ZyUpFactoryRecordThread(){}
	
	/**
	 * @param beginow 开始行数
	 * @param endow 结束行数
	 * @param sheet sheet页
	 */
	public ZyUpFactoryRecordThread(int begin,int end,XSSFSheet sheet){
		this.begin = begin;
		this.end = end;
		this.sheet = sheet;
	}
	
	public void run(){
		long beginDate = System.currentTimeMillis();
		System.out.println("线程:"+Thread.currentThread().getId()+"\t,开始解析："+ begin +"\t至\t"+ end);
		try {
			ZyFactoryRecordEntity zyFactoryRecord = null;
			String serialNum = "";
			List<ZyFactoryRecordEntity> zyFactoryRecordList = new ArrayList<ZyFactoryRecordEntity>();
			List<ZyFactoryRecordHEntity> zyFactoryRecordHList = new ArrayList<ZyFactoryRecordHEntity>();
			for(int r = begin; r <= end; r++) {
				XSSFRow row = sheet.getRow(r);
				String maxVersionSql = "SELECT MAX(t.version) version FROM zy_factory_record_his t ";
				String version = getVersion(maxVersionSql);
				if(null != row){
					zyFactoryRecord = new ZyFactoryRecordEntity();
					zyFactoryRecord.setSearchArgument(ExcelUtil.getCellValue(row.getCell(0)));
					zyFactoryRecord.setSoldToBp(ExcelUtil.getCellValue(row.getCell(1)));
					zyFactoryRecord.setSoldToContact(ExcelUtil.getCellValue(row.getCell(2)));
					zyFactoryRecord.setItem(ExcelUtil.getCellValue(row.getCell(3)));
					serialNum = ExcelUtil.getCellValue(row.getCell(4)).replace(".", "");
					zyFactoryRecord.setSerialNum(serialNum);
					zyFactoryRecord.setItemDesc(ExcelUtil.getCellValue(row.getCell(5)));
					zyFactoryRecord.setAlternateSerialNr(ExcelUtil.getCellValue(row.getCell(6)));
					
//					zyFactoryRecord.setCreationDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(7)),ft));
//					zyFactoryRecord.setCreationTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(8)),ft));
					zyFactoryRecord.setCreationDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(7)), DateUtils.sdf_date_1));
					zyFactoryRecord.setCreationTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(8)), DateUtils.sdf_date_1));
					
					zyFactoryRecord.setSerializedItemGroup(ExcelUtil.getCellValue(row.getCell(9)));
					zyFactoryRecord.setSoldToAddress(ExcelUtil.getCellValue(row.getCell(10)));
					zyFactoryRecord.setWarranty(ExcelUtil.getCellValue(row.getCell(11)));
					zyFactoryRecord.setTermId(ExcelUtil.getCellValue(row.getCell(12)));
					
//					zyFactoryRecord.setInstallationDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(13)),ft));
//					zyFactoryRecord.setInstallationTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(14)),ft));
					zyFactoryRecord.setInstallationDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(13)), DateUtils.sdf_date_1));
					zyFactoryRecord.setInstallationTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(14)), DateUtils.sdf_date_1));
					
					zyFactoryRecord.setCoveredByContract(ExcelUtil.getCellValue(row.getCell(15)));
					zyFactoryRecord.setCoveredByContrEnum(ExcelUtil.getCellValue(row.getCell(16)));
					zyFactoryRecord.setRevision(ExcelUtil.getCellValue(row.getCell(17)));
					zyFactoryRecord.setDummySerialNrEnum(ExcelUtil.getCellValue(row.getCell(18)));
					zyFactoryRecord.setDummySerialNr(ExcelUtil.getCellValue(row.getCell(19)));
					zyFactoryRecord.setBuyFromBp(ExcelUtil.getCellValue(row.getCell(20)));
					
//					zyFactoryRecord.setDeliveryDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(21)),ft));
//					zyFactoryRecord.setDeliveryTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(22)),ft));
					zyFactoryRecord.setDeliveryDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(21)), DateUtils.sdf_date_1));
					zyFactoryRecord.setDeliveryTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(22)), DateUtils.sdf_date_1));
					
					zyFactoryRecord.setPrj(ExcelUtil.getCellValue(row.getCell(23)));
					zyFactoryRecord.setPrjElementActivity(ExcelUtil.getCellValue(row.getCell(24)));
					zyFactoryRecord.setSalesOrder(ExcelUtil.getCellValue(row.getCell(25)));
					zyFactoryRecord.setSalesOrderLine(ExcelUtil.getCellValue(row.getCell(26)));
					zyFactoryRecord.setSpecificationA(ExcelUtil.getCellValue(row.getCell(27)));
					zyFactoryRecord.setSpecificationB(ExcelUtil.getCellValue(row.getCell(28)));
					zyFactoryRecord.setMachine(ExcelUtil.getCellValue(row.getCell(29)));
					zyFactoryRecord.setCluster(ExcelUtil.getCellValue(row.getCell(30)));
					zyFactoryRecord.setStatusEnum(ExcelUtil.getCellValue(row.getCell(31)));
					zyFactoryRecord.setStatus(ExcelUtil.getCellValue(row.getCell(32)));
					
//					zyFactoryRecord.setStatusDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(33)),ft));
//					zyFactoryRecord.setStatusTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(34)),ft));
					zyFactoryRecord.setStatusDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(33)), DateUtils.sdf_date_1));
					zyFactoryRecord.setStatusTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(34)), DateUtils.sdf_date_1));
					
					zyFactoryRecord.setTopItem(ExcelUtil.getCellValue(row.getCell(35)));
					zyFactoryRecord.setTopSerialNr(ExcelUtil.getCellValue(row.getCell(36)));
					zyFactoryRecord.setWorkSiteAddress(ExcelUtil.getCellValue(row.getCell(37)));
					zyFactoryRecord.setWorkSiteInfo(ExcelUtil.getCellValue(row.getCell(38)));
					zyFactoryRecord.setContact(ExcelUtil.getCellValue(row.getCell(39)));
					zyFactoryRecord.setServiceDept(ExcelUtil.getCellValue(row.getCell(40)));
					
//					zyFactoryRecord.setRegistrationDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(41)),ft));
//					zyFactoryRecord.setRegistrationTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(42)),ft));
					zyFactoryRecord.setRegistrationDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(41)), DateUtils.sdf_date_1));
					zyFactoryRecord.setRegistrationTime(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(42)), DateUtils.sdf_date_1));
					
					zyFactoryRecord.setCounterReading(ExcelUtil.getCellValue(row.getCell(43)));
					zyFactoryRecord.setMeasurementType(ExcelUtil.getCellValue(row.getCell(44)));
					zyFactoryRecord.setSubcontractor(ExcelUtil.getCellValue(row.getCell(45)));
					zyFactoryRecord.setShipFromBp(ExcelUtil.getCellValue(row.getCell(46)));
					zyFactoryRecord.setShipFromAddress(ExcelUtil.getCellValue(row.getCell(47)));
					zyFactoryRecord.setLot(ExcelUtil.getCellValue(row.getCell(48)));
					
//					zyFactoryRecord.setManufacturingDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(49)),ft));
					zyFactoryRecord.setManufacturingDate(DateUtils.str2Date(ExcelUtil.getCellValue(row.getCell(49)), DateUtils.sdf_date_1));
					
					zyFactoryRecord.setPriority(ExcelUtil.getCellValue(row.getCell(50)));
					zyFactoryRecord.setLogComp(ExcelUtil.getCellValue(row.getCell(51)));
					zyFactoryRecord.setPhysComp(ExcelUtil.getCellValue(row.getCell(52)));
					String isSustainable = ExcelUtil.getCellValue(row.getCell(53));
					zyFactoryRecord.setIsSustainable(isSustainable.equals("") ? 0 : Integer.valueOf(isSustainable));
					String isDeletable = ExcelUtil.getCellValue(row.getCell(54));
					zyFactoryRecord.setIsDeletable(isDeletable.equals("") ? 0 : Integer.valueOf(isDeletable));
					zyFactoryRecordList.add(zyFactoryRecord);
					//保存历史
					ZyFactoryRecordHEntity zyFactoryRecordHEntity = new ZyFactoryRecordHEntity();
					MyBeanUtils.copyBeanNotNull2Bean(zyFactoryRecord, zyFactoryRecordHEntity);
					zyFactoryRecordHEntity.setVersion(version);
					zyFactoryRecordHList.add(zyFactoryRecordHEntity);
					
				}
			}
			systemService.batchSave(zyFactoryRecordList);
			systemService.batchSave(zyFactoryRecordHList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		long endDate = System.currentTimeMillis();
		System.err.println("线程:"+this.getThreadGroup().getName()+"\n结束解析："+ begin +"\n至\n"+ end + "\n本线程 用时："+ (endDate - beginDate) / 1000 +"." + (endDate - beginDate) % 1000 +"秒");
	}
	
	//获得最新版本号
	private String getVersion(String sql){
		String version = "1";
		Map<String, Object> filePathMap = systemService.findOneForJdbc(sql.toString());
		if(filePathMap != null){
			if(StringUtil.isNotEmpty(filePathMap.get("version"))){
				String ver = filePathMap.get("version").toString();
				version = String.valueOf(Integer.valueOf(ver) + 1);
			}
		}
		return version;
	}
	
	public int getBegin() {
		return begin;
	}
	public void setBegin(int begin) {
		this.begin = begin;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public XSSFSheet getSheet() {
		return sheet;
	}
	public void setSheet(XSSFSheet sheet) {
		this.sheet = sheet;
	}
	
	
	
}
